package com.gagakuai.bpm.api.camunda;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gagakuai.bpm.domian.dto.camunda.WorkFlowInstenceDto;
import com.gagakuai.bpm.domian.dto.response.ResponseStatusDto;
import com.gagakuai.bpm.domian.entity.camuda.ActHiActinst;
import com.gagakuai.bpm.domian.entity.camuda.WorkflowDefinetion;
import com.gagakuai.bpm.domian.entity.camuda.WorkflowTaskEntityNew;
import com.gagakuai.bpm.domian.query.camunda.WorkflowQuery;
import com.gagakuai.bpm.service.camunda.ActGeBytearrayService;
import com.gagakuai.bpm.service.camunda.WorkFlowInstenceService;
import com.gagakuai.bpm.service.camunda.WorkFlowTaskNodeService;
import com.gagakuai.bpm.service.camunda.WorkflowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 * @program: mss_server
 * @description:
 * @author: houhong
 * @create: 2022-03-09 17:18
 */
@Api(value = "/bpm/task", tags = {"任务管理:获取任务"})
@RestController
@RequestMapping("/bpm/task")
public class WorkflowTaskQueryController {


    @Resource
    private WorkflowTaskService workflowTaskService;

    @Resource
    private WorkFlowInstenceService workflowInstanceService;

    @Resource
    private WorkFlowTaskNodeService workFlowTaskNodeService;

    @Resource
    private ActGeBytearrayService actGeBytearrayService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "key", value = "")
    })
    @ApiOperation(value = "获取流程实例最大数", notes = "", httpMethod = "GET")
    @GetMapping("/getProcessInstancesNum")
    public ResponseStatusDto<String> getMyProcessInstances(@RequestParam("key") String key) {
        return ResponseStatusDto.success(workflowTaskService.getProcessInstanceNum(key));
    }


    @ApiOperation(value = "获取定义的xml", notes = "", httpMethod = "POST")
    @PostMapping("/getBpmDefineBinaryFile/{procInsId}")
    public ResponseStatusDto<WorkflowDefinetion> getBpmDefineBinaryFile(@PathVariable("procInsId") String procInsId) throws Exception {


        return ResponseStatusDto.success(workflowTaskService.getBpmDefineBinaryFile(procInsId));
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WorkflowQueryDto", name = "workflowQueryDto", value = "")
    })
    @ApiOperation(value = "根据用户获取流程待办任务列表", notes = "需要传入用户账号和所用所在的组", httpMethod = "POST")
    @PostMapping("/getMyTodoTaskList")
    public ResponseStatusDto<IPage<WorkflowTaskEntityNew>> getTodoTaskList(@RequestBody @Validated WorkflowQuery workflowQueryDto) throws Exception {

        return ResponseStatusDto.success(workflowTaskService.getTodoTaskListByUser(workflowQueryDto));
    }


    @ApiOperation("获取流程完成任务列表")
    @PostMapping("/getMyCompleteTaskList")
    public ResponseStatusDto<IPage<WorkflowTaskEntityNew>> getCompleteTaskList(@RequestBody @Validated WorkflowQuery workflowQueryDto) {

        return ResponseStatusDto.success(workflowTaskService.getCompleteTaskListByUser(workflowQueryDto));
    }


    @ApiOperation("获取我的申请")
    @PostMapping("/getMyApplyProcess/{userId}")
    public ResponseStatusDto<IPage<WorkFlowInstenceDto>> getMyApplyProcess(
            @PathVariable("userId") String userId,
            @RequestBody @Validated WorkflowQuery workflowQueryDto) throws Exception {

        return ResponseStatusDto.success(workflowInstanceService.getApplyProcessByUer(workflowQueryDto, userId));
    }


    @ApiOperation("流程轨迹")
    @PostMapping("/getprocesshandleHistory/{processId}")
    public ResponseStatusDto<IPage<ActHiActinst>> getProcessHandleHisTory(
            @PathVariable("processId") String processId,
            @RequestBody @Validated WorkflowQuery workflowQueryDto) throws Exception {

        return ResponseStatusDto.success(workFlowTaskNodeService.getTraceByProcesssIdPage(workflowQueryDto, processId));

    }

    @ApiOperation("根据流程实列获取正在运行的任务数量")
    @GetMapping("/getRuTaskNum/{processId}/{taskDefKey}")
    public ResponseStatusDto<Integer> getRuTaskNumByProcessIns(
            @PathVariable("processId") String processId,
            @PathVariable("taskDefKey") String taskDefKey) throws Exception {


        Integer rutaaskNumByProcessIns = workflowTaskService.getRutaaskNumByProcessIns(processId, taskDefKey);

        ResponseStatusDto<Integer> success = ResponseStatusDto.success(rutaaskNumByProcessIns);

        return success;

    }


    @ApiOperation("根据流程实列获取正在运行的任务数量")
    @GetMapping("/getRuTaskDefKey/{taskId}")
    public ResponseStatusDto<String> getRuTaskDefKey(
            @PathVariable("taskId") String taskId) throws Exception {


        String taskDefKey = workflowTaskService.getTaskDefKeyBytaskId(taskId);

        ResponseStatusDto<String> success = ResponseStatusDto.success(taskDefKey);

        return success;

    }

    //deleteRuTaskNum
    @ApiOperation("根据流程实列删除正在运行的活跃任务")
    @GetMapping("/deleteRuTaskNum/{processId}")
    public ResponseStatusDto<Integer> deleteRuTaskNumByProcessIns(
            @PathVariable("processId") String processId) throws Exception {

        Integer rutaaskNumByProcessIns = workflowTaskService.deleteRutaaskNumByProcessIns(processId);

        ResponseStatusDto<Integer> success = ResponseStatusDto.success(rutaaskNumByProcessIns);

        return success;

    }



    @ApiOperation("根据流程实列获取正在运行的活跃任务")
    @GetMapping("/getRuTaskNumByTaskId/{taskId}")
    public ResponseStatusDto<Integer> getRuTaskNumByTaskId(
            @PathVariable("taskId") String taskId) throws Exception {

        int numByTaskId = workflowTaskService.getNumByTaskId(taskId);
        ResponseStatusDto<Integer> success = ResponseStatusDto.success(numByTaskId);

        return success;

    }


    //deleteRuTaskNum
    @ApiOperation("查询正在运行的活跃任务")
    @GetMapping("/getRuTaskNumByTaskIdAndUserId/{processId}/{taskId}/{userId}")
    public ResponseStatusDto<Integer> getRuTaskNumByTaskIdAndUserId(
            @PathVariable("processId") String processId,
            @PathVariable("taskId") String taskId,
            @PathVariable("userId") String userId) throws Exception {

        int numByTaskId = workFlowTaskNodeService.getRuTaskNumByuserName(processId, taskId, userId);
        ResponseStatusDto<Integer> success = ResponseStatusDto.success(numByTaskId);

        return success;

    }


    //deleteRuTaskNum
    @ApiOperation("查询正在运行的活跃任务")
    @GetMapping("/getRuTaskIdByProcessAndAssigne/{processIns}/{userId}")
    public ResponseStatusDto<String> getRuTaskByTaskIdAndUserId(
            @PathVariable("processIns") String processId,
            @PathVariable("userId") String userId) throws Exception {

        String taskId = workFlowTaskNodeService.getTaskIdByUserAndProcessIn(processId, userId);
        ResponseStatusDto<String> success = ResponseStatusDto.success(taskId);

        return success;

    }

    ///bpm/task/getProcessStatus/{processIns}
    //deleteRuTaskNum
    @ApiOperation("查询正在运行的活跃任务")
    @GetMapping("/getProcessStatus/{processIns}")
    public ResponseStatusDto<String> getProcessStatus(
            @PathVariable("processIns") String processId) throws Exception {

        String status = workFlowTaskNodeService.getProcessStatus(processId);
        ResponseStatusDto<String> success = ResponseStatusDto.success(status);

        return success;

    }
}